package com.aqie.factory.data.helper;

import android.text.TextUtils;
import android.util.Log;

import com.aqie.common.factory.data.DataSource;
import com.aqie.factory.Factory;
import com.aqie.factory.model.api.RspModel;
import com.aqie.factory.model.api.account.AccountRspModel;
import com.aqie.factory.model.api.account.LoginModel;
import com.aqie.factory.model.api.account.RegisterModel;
import com.aqie.factory.model.db.User;
import com.aqie.factory.net.Network;
import com.aqie.factory.net.RemoteService;
import com.aqie.factory.persistence.Account;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 网络请求 注册逻辑
 */
public class AccountHelper {
    /**
     * 注册的接口，异步的调用
     *
     * @param model    传递一个注册的Model进来
     * @param callback 成功与失败的接口回送
     */
    public static void register(final RegisterModel model, final DataSource.Callback<User> callback) {
        // 调用Retrofit对我们的网络请求接口做代理
        RemoteService service = Network.remote();
        // 得到一个Call
        Call<RspModel<AccountRspModel>> call = service.accountRegister(model);
        // 异步的请求
        call.enqueue(new AccountRspCallback(callback));

        // mockRegisterRequest(callback);
    }

    // 模拟请求
    private static void mockRegisterRequest(final DataSource.Callback<User> callback) {
        new Thread(){
            @Override
            public void run() {
                super.run();
                try{
                    Thread.sleep(2000);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
                callback.onDataNotAvailable("登录失败");
            }
        }.start();
    }

    /**
     * 登录的调用
     *
     * @param model    登录的Model
     * @param callback 成功与失败的接口回送
     */
    public static void login(final LoginModel model, final DataSource.Callback<User> callback) {
        // 调用Retrofit对我们的网络请求接口做代理
        RemoteService service = Network.remote();
        // 得到一个Call
        Call<RspModel<AccountRspModel>> call = service.accountLogin(model);
        // 异步的请求
        call.enqueue(new AccountRspCallback(callback));
    }

    /**
     * 对设备Id进行绑定的操作
     *
     * @param callback Callback
     */
    public static void bindPush(final DataSource.Callback<User> callback) {
        // 检查是否为空
        String pushId = Account.getPushId();
        if (TextUtils.isEmpty(pushId))
            return;

        // 调用Retrofit对我们的网络请求接口做代理
        RemoteService service = Network.remote();
        Call<RspModel<AccountRspModel>> call = service.accountBind(pushId);
        call.enqueue(new AccountRspCallback(callback));
    }

    /**
     * 请求的回调部分封装
     */
    private static class AccountRspCallback implements Callback<RspModel<AccountRspModel>> {
        final DataSource.Callback<User> callback;

        AccountRspCallback(DataSource.Callback<User> callback) {
            this.callback = callback;
        }

        /**
         * 请求成功返回
         * @param response 返回账户模型,内部使用Gson解析
         */
        @Override
        public void onResponse(Call<RspModel<AccountRspModel>> call, Response<RspModel<AccountRspModel>> response) {
            RspModel<AccountRspModel> rspModel = response.body();
            // 打印消息回送
            // String rspString = SimpleJsonTool.toJson(rspModel); Log.e("TAG",rspString);
            if (rspModel.success()){
                // 1. 拿到实体
                AccountRspModel accountRspModel = rspModel.getResult();
                // 2. 获取个人信息
                User user = accountRspModel.getUser();
                // 3. 数据库写入
                DbHelper.save(User.class, user);

                // 4. 缓存绑定 同步到XML持久化中
                Account.login(accountRspModel);
                if(accountRspModel.isBind()){
                    Account.setBind(true);
                    if (callback != null)
                        callback.onDataLoaded(user);
                }else{
                    callback.onDataLoaded(user);
                    // 进行绑定的唤起
                    bindPush(callback);
                }
            }else{
                Factory.decodeRspCode(rspModel, callback);
            }
        }

        /**
         * 对设备Id进行绑定的操作
         *
         * @param callback Callback
         */
        public static void bindPush(final DataSource.Callback<User> callback) {
            // 检查是否为空
            String pushId = Account.getPushId();
            if (TextUtils.isEmpty(pushId))
                return;

            // 调用Retrofit对我们的网络请求接口做代理
            RemoteService service = Network.remote();
            Call<RspModel<AccountRspModel>> call = service.accountBind(pushId);
            call.enqueue(new AccountRspCallback(callback));
        }

        @Override
        public void onFailure(Call<RspModel<AccountRspModel>> call, Throwable t) {
            Log.e("TAG", t.toString());
            // 网络请求失败
            if (callback != null) {
                callback.onDataNotAvailable("网络错误");
            }
        }
    }
}
